{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.None  2.[30,4]  3.3  4.65"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    1.×  2.×  3.×"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串accbabcdabc\n"
     ]
    }
   ],
   "source": [
    "s = str(input('请输入一个字符串'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('c')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('d')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict1 = {'a':0, 'b':0, 'c':0, 'd':0, 'e':0, 'f':0, 'g':0,\n",
    "         'h':0, 'i':0, 'j':0, 'k':0, 'l':0, 'm':0, 'n':0,\n",
    "         'o':0, 'p':0, 'q':0, 'r':0, 's':0, 't':0, 'u':0,\n",
    "         'v':0, 'w':0, 'x':0, 'y':0, 'z':0}\n",
    "num = 0    #用于指示字符串的字符下标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def function(a, dict1):\n",
    "    for i in a:\n",
    "        if i in dict1:\n",
    "            dict1[i] += 1\n",
    "    return dict1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enumerate\n"
     ]
    }
   ],
   "source": [
    "a = str(input())\n",
    "function(a, dict1)\n",
    "for x in a:\n",
    "    num += 1\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串：enumerate\n",
      "[('n', 1), ('u', 2), ('m', 3), ('r', 5), ('a', 6), ('t', 7), ('e', 8)]\n"
     ]
    }
   ],
   "source": [
    "test = input(\"请输入一个字符串：\")\n",
    "positions = [(ch,index) for index,ch \n",
    "enumerate(test)\n",
    "  if index == test.rindex(ch)]\n",
    "print(positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import reduce"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入包含若干集合的列表[{12},{34}]\n"
     ]
    }
   ],
   "source": [
    "sets = eval(input(\"请输入包含若干集合的列表\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('n', 1), ('u', 2), ('m', 3), ('r', 5), ('a', 6), ('t', 7), ('e', 8)]\n"
     ]
    }
   ],
   "source": [
    "print(positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1. Python 3.x的range()函数返回一个"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "range对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2.表达式“[3] in[1，2，3，4]”的值为"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "3.列表对象的sort()方法用来对列表元素进行原地排序，该函数返回值为"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "4.列表对象的 方法删除首次出现的指定元素，如果列表中不存在要删除的元素，则抛出异常"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "5.假设列表对象aList的值为[3，4，5，6，7，9，11，13，15，17]，那么切片 alist[3:7]得到的值是"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[3,4,5,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "6.在Python中，字典和集合都是用一对分组成，即 和 ，其中 作为界定符，字典的每个元素由两部不允许重复。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "{}  键和值  键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "7.使用字典对象的 方法可以返回字典的“键：值”对，使用字典对象的 方法可以返回字典的“键”，使用字典对象的 方法可以返回字典的\n",
    "“值”。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "items()  keys()  vaiues()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "8.假设有列表a=['name'，‘age'，'sex']和b=[\"Dong'，38，\"Male'，使用一条语句将这两个列表的内容转换为字典，并且以列表a中的元素为“键”，以列表b中的元素为“值”，这个语句可以写为"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "d = {'a':1,'b':2}\n",
    "a = input(\"s\")\n",
    "print (d.get(a,0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "9.假设有一个列表a，现要求从列表a中每3个元素取1个，并且将取到的元素组成新的列表b，可以使用语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "a[::3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "10. 使用列表推导式生成包含10个数字5的列表，语句可以写为"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[5 for i in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "11. (可以、不可以）使用del命令来删除元组中的部分元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "12. 已知列表data中有若干字符串，要求编写程序，对data中的字符串进行过滤，只输出重复字符不超过一半的字符串。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "from functools import reduce\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "13. 编写程序，使用筛选法查找并输出小于1000的所有素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-19-7ec828af6ea5>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-19-7ec828af6ea5>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    include<stdio.h>\u001b[0m\n\u001b[1;37m                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "include<stdio.h>\n",
    "int main(){int num[1001] = {0};\n",
    "    int count = 0;\n",
    "    for (int i = 2; i < 1000; i++) {\n",
    "        if(num[i] == 0){    //是素数\n",
    "            printf(\"%d\\n\", i);\n",
    "            //筛掉素数的倍数\n",
    "            for (int j = i + i; j < 1000 ; j += i) {    //此处用+不用*，是因为+的运算速度更快\n",
    "                count++;    //记录循环运行的次数\n",
    "                num[j] = 1;\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    printf(\"for循环语句执行的次数：%d\\n\", count);\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "14.为什么应尽量从列表的尾部进行元素的增加与删除操作？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当列表从中间或从头增加或删除元素时，列表对象自动进行内存扩展或收缩，从而保证元素之间没有缝隙，但这涉及到列表元素的大量的移动，效率较低，应尽量从列表尾部进行元素的增加与删除操作以提高处理速度."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "15. 编写程序，生成包含1000个0～100的随机整数，并统计每个元素的出现次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unexpected EOF while parsing (<ipython-input-20-7722e2f56054>, line 8)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-20-7722e2f56054>\"\u001b[1;36m, line \u001b[1;32m8\u001b[0m\n\u001b[1;33m    print('元素{0}的出现次数为{1}'.\u001b[0m\n\u001b[1;37m                           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "x=[random.randint(0,101) for i in range(1000)]\n",
    "#返回一个range对象,但是range对象需要转换为list对象\n",
    "\n",
    "\n",
    "for i in x:\n",
    "    print('元素{0}的出现次数为{1}'.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "16.编写程序，用户输入一个列表和2个整数作为下标，然后输出列表中介于2个下标闭区间之间的元素组成的子列表。例如用户输入[1，2，3，4，5，6]和2，5，程序输出[3，4，\n",
    "5，6]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表：[1,2,3,4,5,6]\n"
     ]
    }
   ],
   "source": [
    "list = eval(input(\"请输入一个列表：\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = eval(input(\"请输入第一个下标：\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = eval(input(\"请输入第二个小标：\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if a>b:\n",
    "    for i in range(b,a):\n",
    "        print(list[b:a])\n",
    "        break\n",
    "elif a<b:\n",
    "    for i in range(a,b):\n",
    "        print(list[a:b])\n",
    "        break\n",
    "elif a==b:\n",
    "    print([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "17.设计一个字典，并编写程序，用户输入内容作为“键”，然后输出字典中对应的“值”，如果用户输入的“键”不存在，则输出“您输入的键不存在！”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-6-61eeaf30855f>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-6-61eeaf30855f>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    a = {‘name’:‘li’,‘age’:‘26’,‘gender’:‘male’} #一个列表，键与值相对应。 #b = input (\"请输入键: \") # 请输入键 #print(a.get(b,“你输入的键不存在”))\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "a = {‘name’:‘li’,‘age’:‘26’,‘gender’:‘male’} #一个列表，键与值相对应。 #b = input (\"请输入键: \") # 请输入键 #print(a.get(b,“你输入的键不存在”)) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "18.编写程序，生成包含20个随机数的列表，然后将前10个元素升序排列，后10个元素降序排列，并输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 6, 7, 8, 9, 11, 13]\n"
     ]
    }
   ],
   "source": [
    "a=[1,2,3,4,6,8,7,9,11,13,10,12,15,17,14,18,19,20,5]\n",
    "print (sorted(a[:11]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
